﻿using System;
using VIT.Library.Web;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;
using VIT.Library.Images;

namespace VIT.Web.Admin
{
    public partial class SupplierEdit : VIT.Web.UI.VITComponent
    {
        Provider.ProductSupplier obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.ProductSupplier();

            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = 0;
            }

            if (obj.Id == 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadData();
            }
        }

        #region Methods
        private void loadData()
        {
            obj.GetSingle();
            if (obj.Id > 0)
            {
                txtTitle.Text = obj.Name;               
                txtDescription.Text = obj.Description;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
                if (!string.IsNullOrEmpty(obj.Image))
                {
                    img.Visible = true;
                    img.ImageUrl = URLRoot + "Uploads/Images/Suppliers/" + obj.Image;
                    img.AlternateText = obj.Image;
                }
            }
        }

        private void save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtTitle, new List<object> {"required"}},
                                                        {txtOrder, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool imgvalid = true;                
                if (fupImg.HasFile)
                {
                    if (MainCore.checkFile(fupImg.FileName, "isImage"))
                    {
                        string oldImg = img.AlternateText;
                        obj.Image = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupImg.FileName);
                        try
                        {
                            // save
                            fupImg.SaveAs(URLBaseDir + "Uploads/Images/Suppliers/" + obj.Image);

                            // delete
                            System.IO.File.Delete(URLBaseDir + "Uploads/Images/Suppliers/" + oldImg);
                        }
                        catch (System.NotSupportedException ex)
                        {
                            throw ex;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        imgvalid = false;
                        setTrueVisible(lblHinhKoDung);
                    }
                }
                else obj.Image = img.AlternateText;

                if (imgvalid)
                {
                    obj.Name = txtTitle.Text;
                    obj.Description = txtDescription.Text;
                    obj.Orders = int.Parse(txtOrder.Text);
                    obj.Published = chkPublish.Checked;

                    if (obj.Save()) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
                else setTrueVisible(lblHinhKoDung);
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblHinhKoDung.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Product", "SupplierList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Product", "SupplierList");
        }
        #endregion
    }
}
